﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;

namespace iTransact.Helpers
{
	internal static class DataAnnotationsValidationRunner
	{
		public static IEnumerable<Error> GetErrors(object instance)
		{
			return from prop in TypeDescriptor.GetProperties(instance).Cast<PropertyDescriptor>()
			       from attribute in prop.Attributes.OfType<ValidationAttribute>()
			       where !attribute.IsValid(prop.GetValue(instance))
			       select new Error(prop.Name, attribute.FormatErrorMessage(prop.Name));
		}
	}
}
